package com.hfzy.ihk.web.freeswitchManage.controller;


import com.hfzy.ihk.facade.freeswitchManage.enums.CompanyEnum;
import com.hfzy.ihk.facade.freeswitchManage.enums.ComputerRoomEnum;
import com.hfzy.ihk.facade.freeswitchManage.enums.EquipmentEnum;
import com.hfzy.ihk.facade.freeswitchManage.enums.GatewayTypeEnum;
import com.hfzy.ihk.common.web.enums.WebResult;
import com.hfzy.ihk.common.web.enums.WebResultCode;
import com.hfzy.ihk.facade.freeswitchManage.entity.Gateway;
import com.hfzy.ihk.web.freeswitchManage.biz.GatewayInfoBiz;
import com.hfzy.ihk.web.freeswitchManage.mapper.GatewayMapper;
import com.hfzy.ihk.web.freeswitchManage.vo.GatewayVo;
import com.hfzy.ihk.web.oauth.client.table.PageTableHandler;
import com.hfzy.ihk.web.oauth.client.table.PageTableRequest;
import com.hfzy.ihk.web.oauth.client.table.PageTableResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wws
 * @since 2019-01-25
 */
@RestController
@RequestMapping("/gatewayinfo")
@Slf4j
public class GatewayinfoController {


    @Autowired
    GatewayMapper gatewayMapper;

    @Autowired
    GatewayInfoBiz gatewayInfoBiz;
    @RequestMapping("page")
    public ModelAndView page(ModelAndView modelAndView){
        modelAndView.setViewName("page/gatewayinfo/gatewayList");
        modelAndView.addObject("belongCompanys",CompanyEnum.values());
        return modelAndView;

    }

    @RequestMapping("addPage")
    public ModelAndView addPage(ModelAndView modelAndView){
        modelAndView.setViewName("page/gatewayinfo/addGateway");

        //公司枚举
        modelAndView.addObject("companys", CompanyEnum.values());

        //机房
        modelAndView.addObject("computerRooms", ComputerRoomEnum.values());

        //设备商
        modelAndView.addObject("equipments", EquipmentEnum.values());

        //网关类型
        modelAndView.addObject("gatewayTypes", GatewayTypeEnum.values());

        return modelAndView;

    }

    @RequestMapping("editPage")
    public ModelAndView editPage(ModelAndView modelAndView,Long id){
        modelAndView.setViewName("page/gatewayinfo/editGateway");

        //公司枚举
        modelAndView.addObject("companys", CompanyEnum.values());

        //机房
        modelAndView.addObject("computerRooms", ComputerRoomEnum.values());

        //设备商
        modelAndView.addObject("equipments", EquipmentEnum.values());

        //网关类型
        modelAndView.addObject("gatewayTypes", GatewayTypeEnum.values());

        //根据id查询出当前数据
        Gateway gateway = gatewayMapper.selectById(id);
        modelAndView.addObject("gateway",gateway);
        return modelAndView;

    }

    @RequestMapping("listGateways")
    public PageTableResponse listGateways(PageTableRequest request) {
        request.setOffset((request.getOffset()-1)*request.getLimit());
        return new PageTableHandler(new PageTableHandler.CountHandler() {

            @Override
            public int count(PageTableRequest request) {

                return gatewayMapper.count(request.getParams());
            }
        }, new PageTableHandler.ListHandler() {

            @Override
            public List<Gateway> list(PageTableRequest request) {
                List<Gateway> list = gatewayMapper.list(request.getParams(), request.getOffset(), request.getLimit());
                return list;
            }
        }).handle(request);
    }

    @RequestMapping("delGateWay")
    public Map<String,Object> delGateWay(Long id){
        String rs = "0";
        String rs1="0";
        Map<String,Object> map = new HashMap<>();
        try {
             gatewayInfoBiz.del(id);
            rs1 = "DEL_OK";
            rs = "1";
        }catch (Exception e){
            log.error(">>>>>>>>删除设备信息失败",e);
            rs = "-1";
        }
        map.put(WebResultCode.CODE.toStr(), rs);
        map.put(WebResultCode.MSG.toStr(),WebResult.fromStr(rs1).getDesc());
        return map;

    }

    @RequestMapping("saveGateway")
    public Map<String,Object> saveGateway(GatewayVo gatewayVo){
        String rs = "0";
        String msg = "";
        Map<String,Object> map = new HashMap<>();
        try{
            gatewayInfoBiz.saveGateway(gatewayVo);
            rs = "1";

        }catch (Exception e){
            log.error(">>>>>>>>保存设备信息失败",e);
            rs = "-1";
            msg = e.getMessage();

        }
        map.put(WebResultCode.CODE.toStr(), rs);
        map.put(WebResultCode.MSG.toStr(),!"".equals(msg)?msg:WebResult.fromStr(rs).getDesc());
        return map;

    }
}
